import {
  HTTP
} from "../utils/http-p.js";

export class KeywordModel extends HTTP {

  key = "q";
  maxLenght = 8;

  getHistory() {
    const keyWords = wx.getStorageSync(this.key);
    if (!keyWords) {
      return [];
    }
    return keyWords;
  }

  getHot() {
    return this.request({
      url: '/book/hot_keyword'
    })
  }

  addToHistory(keyword) {
    if (!keyword) return;
    var keyWords = this.getHistory();
    const has = keyWords.includes(keyword);
    if (!has) {
      const lenght = keyWords.length;
      if (lenght >= this.maxLenght) {
        keyWords.pop();
      }
      keyWords.unshift(keyword);
      wx.setStorageSync(this.key, keyWords)
    }
  }

}